﻿using log4net;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMCarView.xaml
  /// </summary>
  public partial class DMCarView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMCarView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public ObservableCollection<CarModel> CarList { get; set; }
    public CarModel SelectedCar { get; set; }
    public DMCarView()
    {
      InitializeComponent();
      this.DataContext = this;

      CarList = new ObservableCollection<CarModel>(CarModel.SelectAllCar());

      ((GridViewComboBoxColumn)this.radGridView.Columns["Customer"]).ItemsSource = CustomerModel.SelectCustomerByType("运输");
    }
    private void RadGridView_Deleting(object sender, GridViewDeletingEventArgs e)
    {
      InfoBar = string.Empty;
      foreach (CarModel car in e.Items)
      {
        int ret = CarModel.SelectUsedCar(car);

        if (ret > 0)
        {
          InfoBar = $"{car.PlateNo} 有关联的称重记录，无法删除！";
          e.Cancel = true;
          return;
        }
        if (ret < 0)
        {
          InfoBar = $"删除失败，请查看系统日志！";
          e.Cancel = true;
          return;
        }
        if (ret == 0)
        {
          ret = CarModel.DeleteCar(car);
          if (ret > 0)
          {
            InfoBar = $"车辆 {car.PlateNo} 已删除";
            log.Info(InfoBar);
          }
          else if (ret < 0)
          {
            InfoBar = $"删除失败，请查看系统日志！";
          }
          return;
        }
      }
    }

    private void RadRadioButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      CarModel car = ((RadRadioButton)sender).DataContext as CarModel;
      CarModel.UpdateCar(car);
      if (car.IsValid)
      {
        InfoBar = $"启用车辆{car.PlateNo}";
        log.Info(InfoBar);
      }
      else
      {
        InfoBar = $"禁用车辆{car.PlateNo}";
        log.Info(InfoBar);
      }

      CarList = new ObservableCollection<CarModel>(CarModel.SelectAllCar());
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      RadButton btn = sender as RadButton;
      if (btn.Content.ToString() == "编辑" && SelectedCar == null)
      {
        InfoBar = "请选择一条记录";
        return;
      }
      else if (btn.Content.ToString() == "新增")
      {
        SelectedCar = null;
      }

      RadWindow window = new DMCarDetailView(SelectedCar);
      window.Owner = this;

      window.ShowDialog();

      CarList = new ObservableCollection<CarModel>(CarModel.SelectAllCar());
    }

    private void RadWatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
      RadWatermarkTextBox textBox = sender as RadWatermarkTextBox;
      var cl = CarModel.SelectCar(textBox.Text);
      if (cl.Count > 0)
      {
        var car = CarList.FirstOrDefault(c => c.PlateNo.Contains(cl[0].PlateNo));
        radGridView.SelectedItem = car;
        radGridView.ScrollIntoView(car);
        InfoBar = string.Empty;
      }
      else
      {
        InfoBar = $"找不到车牌号{textBox.Text}";
      }
    }
  }
}
